%support vector manifold learning
clear all;
clc;

load mnist0v1;

X = double(data(1:100,:)); %size n, both labeled and unlabeled data
Y = double(label(1:100)); %size l < n, labels

l = size(Y,1);
n = size(X,1);

Xtr = X(1:l,:);
Xts = X(l+1:n,:);

k = 3;
lambda = 10;

[IDX,D] = knnsearch(Xtr,Xtr,'K',k);
D2 = D.^2;
IDXvec = reshape(IDX,[],1);
Dvec = reshape(D2,[],1);
Ivec = reshape(repmat((1:l)',1,k),[],1);

cvx_begin sdp
    variable K(n,n) symmetric
    variable t
    variable bet(l,1)
    
    minimize (t-lambda*trace(K)+lambda*sum(sum(K))/n)
    subject to
        K >= 0
        bet >= 0
        Q = (Y*Y').*K(1:l,1:l)
        [Q (Y+bet+ones(l,1)); (Y+bet+ones(l,1))' t] >= 0
        DK = diag(K)
        DKvec = reshape(repmat(DK,1,k),[],1)
        DKvec + DK(IDXvec) - 2*K(sub2ind(Ivec,IDXvec)) <= Dvec
cvx_end